package pers.leeyns.rd.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import pers.leeyns.rd.R;
import pers.leeyns.rd.number.RandomNumberGenerator;

/**  
 * @description: 随机生成日期
 * @author leeyns
 * @date 2016年6月28日
 * @version V1.0  
 */
public class DateRandomGenerator extends RandomNumberGenerator{
	private static final long serialVersionUID = -7052560299149041742L;
	private final static long _START = 1970*365*24*60*60*1000;
	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
	public static DateRandomGenerator build(){
		return new DateRandomGenerator();
	}
	
	public  Date generator(){
		this.setSeed(_START);
		return new Date(nextLong());
	}
	
	public  Date generator(String beginDate,String  endDate){
		try {
			long start = format.parse(beginDate).getTime();
			long end = format.parse(endDate).getTime();//构造结束日期  
			long dist = random(start, end);
			return new Date(dist);
		} catch (ParseException e) {
			return null;
		}
	}
	
	private static long random(long begin,long end){ 
		long rtn = begin + (long)(Math.random() * (end - begin)); 
		//如果返回的是开始时间和结束时间，则递归调用本函数查找随机值 
		if(rtn == begin || rtn == end){ 

		return random(begin,end); 
			
		}
		return rtn;
	}
	
	public static void main(String[] args) {
		while(true)
			System.out.println(R.date().generator("2016-05-10 00:00:00.000", "2016-06-30 00:00:00.000"));
	}
}
